// store/index.js
import { createStore } from 'vuex'
import login from './modules/login.js'
import * as api from '@/api/index'
function getter(obj, arr) {
  return arr.length === 0 ? obj : getter(typeof obj === 'undefined' ? undefined : obj[arr[0]], arr.slice(1))
}
export default createStore({
  state: {
    pageName: 'home',
    customer: '',
    province: '0',
    companyList: [],
    areaList: [],
    leftTree: [],
    leftTreeId: '',
    leftTreeObj: '',
    totalCount: {
      // //查询月份")
      month: (new Date().getFullYear())+'-'+(new Date().getMonth() + 1).toString().padStart(2, '0'),
      //用电周期开始时间")
      startDate: '',
      //用电周期截止时间")
      endDate: '',
      //抄表日期")
      estimateDate: '',
      //本月用电-电量")
      monthQuantities: '',
      //本月用电-电量-同比")
      monthQuantitiesSRatio: '',
      //本月用电-电量-同比箭头 -1为下 1为上 0为没有")
      monthQuantitiesSRatioArrow: '',
      //本月用电-电量-环比")
      monthQuantitiesCRatio: '',
      //本月用电-电量电费-环比箭头 -1为下 1为上 0为没有")
      monthQuantitiesCRatioArrow: '',
      //本月用电-电量电费")
      monthQuantitiesCost: '',
      //本月用电-电量电费-同比")
      monthQuantitiesCostSRatio: '',
      //本月用电-电量电费-同比箭头 -1为下 1为上 0为没有")
      monthQuantitiesCostSRatioArrow: '',
      //本月用电-电量电费-环比")
      monthQuantitiesCostCRatio: '',
      //本月用电-电量电费-环比箭头 -1为下 1为上 0为没有")
      monthQuantitiesCostCRatioArrow: '',
      //本月实际功率因数-功率因数")
      pfc: '',
      //本月实际功率因数-考核")
      evaluation: '',
      //本月实际功率因数-同比")
      pfcSRatio: '',
      //本月实际功率因数-同比箭头")
      pfcSRatioArrow: '',
      //本月实际功率因数-环比")
      pfcCRatio: '',
      //本月实际功率因数-环比箭头")
      pfcCRatioArrow: '',
      //碳排放-碳排放量")
      monthCarbonEmission: '',
      //碳排放-同比")
      monthCarbonEmissionSRatio: '',
      //碳排放-同比箭头")
      monthCarbonEmissionSRatioArrow: '',
      //碳排放-环比")
      monthCarbonEmissionCRatio: '',
      //碳排放-环比箭头")
      monthCarbonEmissionCRatioArrow: ''
    },
    //电量结构
    devicesQuantityCategory: {
      spireQuantity: 0,//尖
      peakQuantity: 0,//峰
      flatQuantity: 0,//平
      valleyQuantity: 0,//谷
      deepValleyQuantity: 0//深谷
    },
    //电费结构
    devicesCostCategory:{
      quantityCost: 0,//电量电费
      fundamentalCost: 0,//基本电费
      adjustedCost: 0,//力调电费
      attachedCost: 0,//附加电费
      totalCost: 0//总电费
    },
    //用能概况 本月电量电费走势数据
    devicesDailyQuantityCost:[],
    //详细数据
    ddidList:[],
    ddidListTime: {
      firstDateTime: "",
      lastDateTime: ""
    },
    analogList: [
      {
        label: '基本参数',
        value: 'jbcs',
        analog: [
          { value: "yggl", label: "有功功率", code: "Pa,Pb,Pc,P", codeName: "A相,B相,C相,总" },
          { value: "wggl", label: "无功功率", code: "Qa,Qb,Qc,Q", codeName: "A相,B相,C相,总" },
          { value: "szgl", label: "视在功率", code: "Sa,Sb,Sc,S", codeName: "A相,B相,C相,总" },
          { value: "dl", label: "电流", code: "Ia,Ib,Ic,In", codeName: "A相,B相,C相,零序" },
          { value: "dy", label: "电压", code: "Ua,Ub,Uc,Uab,Ubc,Uca", codeName: "A相,B相,C相,AB线,BC线,AC线" },
          { value: "pl", label: "频率", code: "Freq", codeName: "频率" },
        ],
      },
      {
        label: '电量',
        value: 'dl',
        analog: [
          {
            value: "ljygdl",
            label: "累计有功电量",
            code: "ljygdl",
            codeName: "累计有功电量"
          },
          {
            value: "ygdl",
            label: "有功电量",
            code: "ygdl",
            codeName: "有功电量"
          },
          {
            value: "wgdl",
            label: "无功电量",
            code: "wgdl",
            codeName: "无功电量"
          },
          {
            value: "bmz",
            label: "表码值",
            code: "bmz",
            codeName: "正向有功电度,反向有功电度"
          }
        ],
      },
      {
        label: '电能质量',
        value: 'dnzl',
        analog: [
          {
            value: "glys",
            label: "功率因数",
            code: "PFa,PFb,PFc,PF",
            codeName: "A相,B相,C相,总"
          },
          {
            value: "dlbphd",
            label: "电流不平衡度",
            code: "UNBL_I",
            codeName: "电流不平衡度"
          },
          {
            value: "dybphd",
            label: "电压不平衡度",
            code: "UNBL_U",
            codeName: "电压不平衡度"
          }
        ],
      },
      {
        label: '电流畸变',
        value: 'dljb',
        analog: [
          {
            value: "zjbl",
            label: "总畸变率",
            code: "THD_Ia,THD_Ib,THD_Ic",
            codeName: "Ia总谐波畸变率,Ib总谐波畸变率,Ic总谐波畸变率"
          },
          {
            value: "qcjbl",
            label: "奇次畸变率",
            code: "TOHD_Ia,TOHD_Ib,TOHD_Ic",
            codeName: "Ia奇谐波畸变率,Ib奇谐波畸变率,Ic奇谐波畸变率"
          },
          {
            value: "ocjbl",
            label: "偶次畸变率",
            code: "TEHD_Ia,TEHD_Ib,TEHD_Ic",
            codeName: "Ia偶谐波畸变率,Ib偶谐波畸变率,Ic偶谐波畸变率"
          },
          {
            value: "HD_I2",
            label: "2次畸变率",
            code: "HD_Ia2,HD_Ib2,HD_Ic2",
            codeName: "Ia谐波2次畸变率,Ib谐波2次畸变率,Ic谐波2次畸变率"
          },
          {
            value: "HD_I3",
            label: "3次畸变率",
            code: "HD_Ia3,HD_Ib3,HD_Ic3",
            codeName: "Ia谐波3次畸变率,Ib谐波3次畸变率,Ic谐波3次畸变率"
          },
          {
            value: "HD_I4",
            label: "4次畸变率",
            code: "HD_Ia4,HD_Ib4,HD_Ic4",
            codeName: "Ia谐波4次畸变率,Ib谐波4次畸变率,Ic谐波4次畸变率"
          },
          {
            value: "HD_I5",
            label: "5次畸变率",
            code: "HD_Ia5,HD_Ib5,HD_Ic5",
            codeName: "Ia谐波5次畸变率,Ib谐波5次畸变率,Ic谐波5次畸变率"
          },
          {
            value: "HD_I6",
            label: "6次畸变率",
            code: "HD_Ia6,HD_Ib6,HD_Ic6",
            codeName: "Ia谐波6次畸变率,Ib谐波6次畸变率,Ic谐波6次畸变率"
          },
          {
            value: "HD_I7",
            label: "7次畸变率",
            code: "HD_Ia7,HD_Ib7,HD_Ic7",
            codeName: "Ia谐波7次畸变率,Ib谐波7次畸变率,Ic谐波7次畸变率"
          },
          {
            value: "HD_I8",
            label: "8次畸变率",
            code: "HD_Ia8,HD_Ib8,HD_Ic8",
            codeName: "Ia谐波8次畸变率,Ib谐波8次畸变率,Ic谐波8次畸变率"
          },
          {
            value: "HD_I9",
            label: "9次畸变率",
            code: "HD_Ia9,HD_Ib9,HD_Ic9",
            codeName: "Ia谐波9次畸变率,Ib谐波9次畸变率,Ic谐波9次畸变率"
          },
          {
            value: "HD_I10",
            label: "10次畸变率",
            code: "HD_Ia10,HD_Ib10,HD_Ic10",
            codeName: "Ia谐波10次畸变率,Ib谐波10次畸变率,Ic谐波10次畸变率"
          },
          {
            value: "HD_I11",
            label: "11次畸变率",
            code: "HD_Ia11,HD_Ib11,HD_Ic11",
            codeName: "Ia谐波11次畸变率,Ib谐波11次畸变率,Ic谐波11次畸变率"
          },
          {
            value: "HD_I12",
            label: "12次畸变率",
            code: "HD_Ia12,HD_Ib12,HD_Ic12",
            codeName: "Ia谐波12次畸变率,Ib谐波12次畸变率,Ic谐波12次畸变率"
          },
          {
            value: "HD_I13",
            label: "13次畸变率",
            code: "HD_Ia13,HD_Ib13,HD_Ic13",
            codeName: "Ia谐波13次畸变率,Ib谐波13次畸变率,Ic谐波13次畸变率"
          },
          {
            value: "HD_I14",
            label: "14次畸变率",
            code: "HD_Ia14,HD_Ib14,HD_Ic14",
            codeName: "Ia谐波14次畸变率,Ib谐波14次畸变率,Ic谐波14次畸变率"
          },
          {
            value: "HD_I15",
            label: "15次畸变率",
            code: "HD_Ia15,HD_Ib15,HD_Ic15",
            codeName: "Ia谐波15次畸变率,Ib谐波15次畸变率,Ic谐波15次畸变率"
          }
        ],
      },
      {
        label: '电压畸变',
        value: 'dyjb',
        analog: [
          {
            value: "dyzjbl",
            label: "电压总畸变率",
            code: "THD_Ua,THD_Ub,THD_Uc",
            codeName: "Ua总谐波畸变率,Ub总谐波畸变率,Uc总谐波畸变率"
          },
          {
            value: "dyqcjbl",
            label: "电压奇次畸变率",
            code: "TOHD_Ua,TOHD_Ub,TOHD_Uc",
            codeName: "Ua奇谐波畸变率,Ub奇谐波畸变率,Uc奇谐波畸变率"
          },
          {
            value: "dyocjbl",
            label: "电压偶次畸变率",
            code: "TEHD_Ua,TEHD_Ub,TEHD_Uc",
            codeName: "Ua偶谐波畸变率,Ub偶谐波畸变率,Uc偶谐波畸变率"
          },
          {
            value: "HD_U2",
            label: "电压2次畸变率",
            code: "HD_Ua2,HD_Ub2,HD_Uc2",
            codeName: "Ua谐波2次畸变率,Ub谐波2次畸变率,Uc谐波2次畸变率"
          },
          {
            value: "HD_U3",
            label: "电压3次畸变率",
            code: "HD_Ua3,HD_Ub3,HD_Uc3",
            codeName: "Ua谐波3次畸变率,Ub谐波3次畸变率,Uc谐波3次畸变率"
          },
          {
            value: "HD_U4",
            label: "电压4次畸变率",
            code: "HD_Ua4,HD_Ub4,HD_Uc4",
            codeName: "Ua谐波4次畸变率,Ub谐波4次畸变率,Uc谐波4次畸变率"
          },
          {
            value: "HD_U5",
            label: "电压5次畸变率",
            code: "HD_Ua5,HD_Ub5,HD_Uc5",
            codeName: "Ua谐波5次畸变率,Ub谐波5次畸变率,Uc谐波5次畸变率"
          },
          {
            value: "HD_U6",
            label: "电压6次畸变率",
            code: "HD_Ua6,HD_Ub6,HD_Uc6",
            codeName: "Ua谐波6次畸变率,Ub谐波6次畸变率,Uc谐波6次畸变率"
          },
          {
            value: "HD_U7",
            label: "电压7次畸变率",
            code: "HD_Ua7,HD_Ub7,HD_Uc7",
            codeName: "Ua谐波7次畸变率,Ub谐波7次畸变率,Uc谐波7次畸变率"
          },
          {
            value: "HD_U8",
            label: "电压8次畸变率",
            code: "HD_Ua8,HD_Ub8,HD_Uc8",
            codeName: "Ua谐波8次畸变率,Ub谐波8次畸变率,Uc谐波8次畸变率"
          },
          {
            value: "HD_U9",
            label: "电压9次畸变率",
            code: "HD_Ua9,HD_Ub9,HD_Uc9",
            codeName: "Ua谐波9次畸变率,Ub谐波9次畸变率,Uc谐波9次畸变率"
          },
          {
            value: "HD_U10",
            label: "电压10次畸变率",
            code: "HD_Ua10,HD_Ub10,HD_Uc10",
            codeName: "Ua谐波10次畸变率,Ub谐波10次畸变率,Uc谐波10次畸变率"
          },
          {
            value: "HD_U11",
            label: "电压11次畸变率",
            code: "HD_Ua11,HD_Ub11,HD_Uc11",
            codeName: "Ua谐波11次畸变率,Ub谐波11次畸变率,Uc谐波11次畸变率"
          },
          {
            value: "HD_U12",
            label: "电压12次畸变率",
            code: "HD_Ua12,HD_Ub12,HD_Uc12",
            codeName: "Ua谐波12次畸变率,Ub谐波12次畸变率,Uc谐波12次畸变率"
          },
          {
            value: "HD_U13",
            label: "电压13次畸变率",
            code: "HD_Ua13,HD_Ub13,HD_Uc13",
            codeName: "Ua谐波13次畸变率,Ub谐波13次畸变率,Uc谐波13次畸变率"
          },
          {
            value: "HD_U14",
            label: "电压14次畸变率",
            code: "HD_Ua14,HD_Ub14,HD_Uc14",
            codeName: "Ua谐波14次畸变率,Ub谐波14次畸变率,Uc谐波14次畸变率"
          },
          {
            value: "HD_U15",
            label: "电压15次畸变率",
            code: "HD_Ua15,HD_Ub15,HD_Uc15",
            codeName: "Ua谐波15次畸变率,Ub谐波15次畸变率,Uc谐波15次畸变率"
          }
        ],
      }
    ]
  },
  mutations: {
    COMPANYGETCOMPANYTREEFORMY(state, val) {
      state.leftTree = val
    },
    SETCREATE(state, val) {
      state.leftTreeId = val
    },
    SETCREATEOBJ(state, val) {
      state.leftTreeObj = val
    },
    CUSTOMERARRAY(state, val) {
      state.companyList = val
    },
    SETPROVINCE(state, val) {
      state.province = val
    },
    PROVINCELIST(state, val) {
      state.areaList = val
    },
    CUSTOMER(state, val) {
      state.customer = val
    },
    TOTALCOUNT(state, val){
      state.totalCount = val.devicesCountDto
      state.devicesQuantityCategory = val.devicesQuantityCategoryDto
      state.devicesCostCategory = val.devicesCostCategoryDto
    },
    DAILYQUANTITIESANDCOST(state, val){
      state.devicesDailyQuantityCost = val
    }
  },
  actions: {
    setCustomer(context, val) {
      context.commit('CUSTOMER', val)
      context.dispatch('companyGetCompanyTreeForMy', { customerId: val })
    },
    setProvince(context, val) {
      context.commit('SETPROVINCE', val)
    },
    companyGetCompanyTreeForMy(context, params) {
      this.state.leftTreeId = ''
      this.state.leftTreeObj= ''
      api.companyGetCompanyTreeForMy(params).then(res => {
        context.commit('COMPANYGETCOMPANYTREEFORMY', res)
        console.log(res)
      })
    },
    getTotalCount(context, timeValue) {
      api.devicesGetTotalCountForMy({month:timeValue,devicesId: this.state.leftTreeId}).then(res => {
        context.commit('TOTALCOUNT', res)
        console.log(res)
      })
    },
    getDailyQuantitiesAndCost(context, timeValue) {
      api.getDailyQuantitiesAndCostForMy({month:timeValue,devicesId: this.state.leftTreeId}).then(res => {
        context.commit('DAILYQUANTITIESANDCOST', res)
        console.log(res)
      })
    },
    setCreate(context, val) {
      if(val.companyId){
        context.commit('SETCREATE', val.id)
        context.commit('SETCREATEOBJ', val)
        context.dispatch('initEnergyuse', {})
      }
    },
    customerGetCustomerListForMy(context, params = { areaId: '0' }) {
      return new Promise(async function (resolve, reject) {
        let companyList = await api.customerGetCustomerListForMy(params)
        if (!companyList) return
        // 存储客户列表
        context.commit('CUSTOMERARRAY', companyList)
        let areaList = await api.areaGetAreaForMy()
        if (!areaList) return
        // 存储城市列表
        context.commit('PROVINCELIST', areaList)
        // 默认选择客户的第一个
        if (companyList && companyList.length) context.dispatch('setCustomer', companyList[0]?.id)
        // 默认选择城市的第一个
        if (areaList && areaList.length) context.dispatch('setProvince', areaList[0]?.id)

        resolve(companyList)
      });
    },
    customerUpdateForMy(context, params = { areaId: '0' }) {
      return new Promise(async function (resolve, reject) {
        let companyList = await api.customerGetCustomerListForMy(params)
        if (!companyList) return
        // 存储客户列表
        context.commit('CUSTOMERARRAY', companyList)
        resolve(companyList)
      });
    },
    initEnergyuse(context,val) {
      if(!this.state.totalCount.month){
        const currentDate = new Date();
        const year = currentDate.getFullYear();
        const month = (currentDate.getMonth() + 1).toString().padStart(2, '0'); // 获取月份并补零

        this.state.totalCount.month = `${year}-${month}`;
      }
      console.log('this.state.pageName:::::::'+this.state.pageName)
      if("home" === this.state.pageName){

      }else if("energyuse" === this.state.pageName){
        context.dispatch('getTotalCount', this.state.totalCount.month)
        context.dispatch('getDailyQuantitiesAndCost', this.state.totalCount.month)
      }else if("realtimemonitor" === this.state.pageName){

      }else if("maximum" === this.state.pageName){

      }

    },
  },
  getters: {
    getTo(state) {
      return function (to) {
        return getter(state, to.split('.'));
      }
    }
  },
  modules: {
    login
  }
})

